package com.wu.yygh.common.exception;

/**
 * @ClassName GlobalExceptionHandler
 * @Author 浮生光
 * @Date 2022/6/29 21:39
 * @Description GlobalExceptionHandler
 * @Version 1.0
 */

import com.wu.yygh.common.result.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 全局异常处理
 */
@ControllerAdvice // controller层的切面
public class GlobalExceptionHandler {

    /**
     * {
     *   "code": 201,
     *   "message": "失败",
     *   "data": null,
     *   "ok": false
     * }
     */
    @ResponseBody
    @ExceptionHandler(Exception.class) // 对指定的异常做特定处理
    public Result error(Exception e) {
        e.printStackTrace();
        return Result.fail();
    }

    /**
     * {
     *   "code": 204,
     *   "message": "数据异常",
     *   "data": null,
     *   "ok": false
     * }
     */
    @ResponseBody
    @ExceptionHandler(YyghException.class)
    public Result error(YyghException e) {
        e.printStackTrace();
        Result<Object> result = new Result<>();
        result.message(e.getMessage());
        result.code(e.getCode());
        return result;
    }

}
